package com.huatai.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.api.R;
import com.huatai.user.dto.ServerInfoDto;
import com.huatai.user.entity.LinkInfo;
import com.huatai.user.entity.ServerInfo;
import com.huatai.user.service.LinkInfoService;
import com.huatai.user.service.ServerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务器连接信息管理接口
 */
@RestController
@RequestMapping("/serverInfo")
@Api(value = "服务器信息管理", tags = "服务器信息管理")
public class ServerInfoController {

	@Autowired
	private ServerInfoService serverInfoService;

	@Autowired
	private LinkInfoService linkInfoService;


	@PostMapping("/add")
	@ApiOperation(value = "新增服务器信息", notes = "新增服务器信息")
	public R addServerInfo(@RequestBody @Validated ServerInfo serverInfo){
		return serverInfoService.saveinfo(serverInfo);
	}

	@GetMapping("/serverStatus")
	@ApiOperation(value = "测试子公司服务是否可用", notes = "测试子公司服务是否可用")
	public R getServerStatus(@RequestParam("serverIp") String serverIp){
		return serverInfoService.getServerStatus(serverIp);
	}

	@DeleteMapping("/delete/{id}")
	@ApiOperation(value = "删除服务器信息")
	public R deleteServerInfo(@PathVariable("id") Long id){
		return serverInfoService.deleteServerInfo(id);
	}

	@GetMapping("/detail/{id}")
	@ApiOperation(value = "获取服务器信息详情")
	public R<ServerInfo> detail(@PathVariable("id") Long id){
		return serverInfoService.detail(id);
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改服务器信息")
	public R updateServerInfo(@RequestBody ServerInfo serverInfo){
		if(serverInfoService.updateServerInfo(serverInfo)){
			return R.success("服务器信息修改成功");
		}
		return R.fail("服务器信息修改失败");
	}

	@PostMapping("/getInfo")
	@ApiOperation(value = "分页查询服务器信息")
	public R<Page<ServerInfo>> getServerInfo(@RequestBody ServerInfoDto pageDto){
		Page page = new Page();
		page.setCurrent(pageDto.getCurrent());
		page.setSize(pageDto.getSize());
		page.setSearchCount(true);
		return serverInfoService.getChangePage(page,pageDto);
	}

	@GetMapping("/getALLInfo")
	@ApiOperation(value = "获取所有服务器信息")
	public R getALLInfo(){
		List<ServerInfo> result = serverInfoService.getALLInfo();
		return R.data(result);
	}

	@GetMapping("/getRemainingLinkNum")
	@ApiOperation(value = "获取服务器剩余渲染路数")
	public R getLink(@RequestParam("serverIp") String serverIp){
		Integer result = linkInfoService.getLink(serverIp);
		return R.data(result);
	}
}
